package com.ltkj.order.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ltkj.common.core.utils.DateUtils;

import com.ltkj.system.api.domain.AppletOrderDetailVo;
import com.ltkj.system.api.domain.OrderDetailQueryDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.order.mapper.TicOrderDetailsMapper;
import com.ltkj.order.domain.TicOrderDetails;
import com.ltkj.order.service.ITicOrderDetailsService;

/**
 * 订单详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-01
 */
@Service
public class TicOrderDetailsServiceImpl implements ITicOrderDetailsService 
{
    @Autowired
    private TicOrderDetailsMapper ticOrderDetailsMapper;

    /**
     * 查询订单详情
     * 
     * @param orderDetailsId 订单详情主键
     * @return 订单详情
     */
    @Override
    public TicOrderDetails selectTicOrderDetailsByOrderDetailsId(Long orderDetailsId)
    {
        return ticOrderDetailsMapper.selectTicOrderDetailsByOrderDetailsId(orderDetailsId);
    }

    /**
     * 查询订单详情列表
     * 
     * @param ticOrderDetails 订单详情
     * @return 订单详情
     */
    @Override
    public List<TicOrderDetails> selectTicOrderDetailsList(TicOrderDetails ticOrderDetails)
    {
        return ticOrderDetailsMapper.selectTicOrderDetailsList(ticOrderDetails);
    }

    /**
     * 新增订单详情
     * 
     * @param ticOrderDetails 订单详情
     * @return 结果
     */
    @Override
    public int insertTicOrderDetails(TicOrderDetails ticOrderDetails)
    {
        ticOrderDetails.setCreateTime(DateUtils.getNowDate());
        return ticOrderDetailsMapper.insertTicOrderDetails(ticOrderDetails);
    }

    /**
     * 修改订单详情
     * 
     * @param ticOrderDetails 订单详情
     * @return 结果
     */
    @Override
    public int updateTicOrderDetails(TicOrderDetails ticOrderDetails)
    {
        ticOrderDetails.setUpdateTime(DateUtils.getNowDate());
        return ticOrderDetailsMapper.updateTicOrderDetails(ticOrderDetails);
    }

    /**
     * 批量删除订单详情
     * 
     * @param orderDetailsIds 需要删除的订单详情主键
     * @return 结果
     */
    @Override
    public int deleteTicOrderDetailsByOrderDetailsIds(Long[] orderDetailsIds)
    {
        return ticOrderDetailsMapper.deleteTicOrderDetailsByOrderDetailsIds(orderDetailsIds);
    }

    /**
     * 删除订单详情信息
     * 
     * @param orderDetailsId 订单详情主键
     * @return 结果
     */
    @Override
    public int deleteTicOrderDetailsByOrderDetailsId(Long orderDetailsId)
    {
        return ticOrderDetailsMapper.deleteTicOrderDetailsByOrderDetailsId(orderDetailsId);
    }

    /**
     * 根据业务主键和类型查询订单详情
     * @param queryDTO
     * @return
     */
    @Override
    public List<AppletOrderDetailVo> queryOrderDetail(OrderDetailQueryDTO queryDTO) {
        List<AppletOrderDetailVo> resultList = ticOrderDetailsMapper.queryOrderDetail(queryDTO);
        for (AppletOrderDetailVo ticOrderDetail : resultList) {
            // 判断当前时间到结束到期时间
            Date createTime = ticOrderDetail.getOrderTime();
            if(createTime != null){
                Date date = DateUtils.timeProcess(createTime, Calendar.MINUTE, 10);
                long l = DateUtils.getDistanceDateTime(new Date() ,date, 4);
                ticOrderDetail.setResidueTimeNum(l);
            }
        }
        return resultList;
    }
}
